package chart;

import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

@SuppressWarnings("serial")
public class LineChart extends ApplicationFrame {
	private int maxData;
	private int minData;
	private String name;
	private String month;
	private int data_1;
	private int data_6;
	private int data_11;
	private int data_16;
	private int data_21;
	private int data_26;
	private int data_30;

	public LineChart(final String title) throws IOException {
		super(title);
		final CategoryDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(chartPanel);
	}

	public CategoryDataset createDataset() {
		// Get the name and data from database
		name = "Gary";
		month = "May";
		data_1 = 145;
		data_6 = 150;
		data_11 = 151;
		data_16 = 150;
		data_21 = 148;
		data_26 = 144;
		data_30 = 149;

		final String series = name + "'s Monthly Statistics";
		final int[] dataArray = new int[] { data_1, data_6, data_11, data_16,
				data_21, data_26, data_30 };
		maxData = getMaxValue(dataArray);
		minData = getMinValue(dataArray);

		// Set the gap to 5 days
		final String date_1 = "1";
		final String date_6 = "6";
		final String date_11 = "11";
		final String date_16 = "16";
		final String date_21 = "21";
		final String date_26 = "26";
		final String date_30 = "30";
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(data_1, series, date_1);
		dataset.addValue(data_6, series, date_6);
		dataset.addValue(data_11, series, date_11);
		dataset.addValue(data_16, series, date_16);
		dataset.addValue(data_21, series, date_21);
		dataset.addValue(data_26, series, date_26);
		dataset.addValue(data_30, series, date_30);
		return dataset;
	}

	public int getMaxValue(int[] numbers) {
		int maxValue = numbers[0];
		for (int i = 1; i < numbers.length; i++) {
			if (numbers[i] > maxValue) {
				maxValue = numbers[i];
			}
		}
		return maxValue;
	}

	public int getMinValue(int[] numbers) {
		int minValue = numbers[0];
		for (int i = 1; i < numbers.length; i++) {
			if (numbers[i] < minValue) {
				minValue = numbers[i];
			}
		}
		return minValue;
	}

	private JFreeChart createChart(final CategoryDataset dataset)
			throws IOException {
		final JFreeChart chart = ChartFactory.createLineChart(
				"Weight v.s. Date in " + month, "Date", "Weight", dataset,
				PlotOrientation.VERTICAL, true, true, false);
		chart.setBackgroundPaint(Color.white);
		final CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.gray);
		plot.setRangeGridlinePaint(Color.white);
		final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setRange((minData - minData / 20), (maxData + maxData / 20));
		final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		renderer.setSeriesLinesVisible(0, true);
		renderer.setSeriesShapesVisible(0, true);
		renderer.setSeriesPaint(0, Color.RED);
		plot.setRenderer(renderer);
		ChartUtilities.saveChartAsJPEG(new java.io.File("Statistic.jpg"),
				chart, 800, 600);
		return chart;
	}

	public static void main(final String[] args) throws IOException {
		final LineChart demo = new LineChart("Statistic Chart");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(false);
	}
}